 <!-- 招聘信息发布页面 -->
<template>
  <div class="addJob">
    <h3 class="title">发布招聘信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="公司名称">
        <el-input :disabled="true" :placeholder="this.username"></el-input>
      </el-form-item>
      <el-form-item label="岗位" prop="post">
        <el-select v-model="ruleForm.post" placeholder="请选择">
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="岗位职责" prop="positionStatement">
        <el-input v-model="ruleForm.positionStatement"></el-input>
      </el-form-item>
      <el-form-item label="任职要求" prop="jobRequirements">
        <el-input v-model="ruleForm.jobRequirements"></el-input>
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-select v-model="ruleForm.education" placeholder="请选择">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工作年限" prop="workYear">
        <el-input v-model="ruleForm.workYear"></el-input>
      </el-form-item>
      <el-form-item label="福利待遇" prop="socialBenefits">
        <el-input v-model="ruleForm.socialBenefits"></el-input>
      </el-form-item>
      <el-form-item label="薪资待遇" prop="pay">
        <el-input v-model="ruleForm.pay"></el-input>
      </el-form-item>
      <el-form-item label="工作地点" prop="workPlace">
        <el-input v-model="ruleForm.workPlace"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >发布</el-button
        >
        <el-button :plain="true" @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import addJob from "../request/index";
export default {
  data() {
    var post = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请选择岗位"));
      } else {
        return callback();
      }
    };
    var positionStatement = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写岗位职责"));
      } else {
        return callback();
      }
    };
    var jobRequirements = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写任职要求"));
      } else {
        return callback();
      }
    };
    var education = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请选择学历"));
      } else {
        return callback();
      }
    };
    var workYear = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写工作年限"));
      } else {
        return callback();
      }
    };
    var socialBenefits = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写福利待遇"));
      } else {
        return callback();
      }
    };
    var pay = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写薪资待遇"));
      } else {
        return callback();
      }
    };
    var workPlace = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写工作地点"));
      } else {
        return callback();
      }
    };
    return {
      userinfo: null,
      username: "",
      ruleForm: {
        post: "", //岗位
        positionStatement: "", //岗位职责
        jobRequirements: "", //任职要求
        education: "", //学历
        workYear: "", //工作年限
        socialBenefits: "", //福利待遇
        pay: "", //薪资
        workPlace: "", //工作地点
      },
      rules: {
        post: [{ validator: post, trigger: "blur" }],
        positionStatement: [{ validator: positionStatement, trigger: "blur" }],
        jobRequirements: [{ validator: jobRequirements, trigger: "blur" }],
        education: [{ validator: education, trigger: "blur" }],
        workYear: [{ validator: workYear, trigger: "blur" }],
        socialBenefits: [{ validator: socialBenefits, trigger: "blur" }],
        pay: [{ validator: pay, trigger: "blur" }],
        workPlace: [{ validator: workPlace, trigger: "blur" }],
      },

      // 企业类型
      options1: [
        {
          value: "IT",
          label: "IT",
        },
        {
          value: "医疗",
          label: "医疗",
        },
        {
          value: "金融",
          label: "金融",
        },
      ],
      // 学历类型
      options2: [
        {
          value: "高中",
          label: "高中",
        },
        {
          value: "大专",
          label: "大专",
        },
        {
          value: "本科",
          label: "本科",
        },
        {
          value: "硕士",
          label: "硕士",
        },
        {
          value: "博士",
          label: "博士",
        },
      ],
    };
  },

  methods: {
    // 发布招聘信息
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //添加请求
          addJob
            .addjob({ //数据
              companyName: this.username,
              post: this.ruleForm.post,
              positionStatement: this.ruleForm.positionStatement,
              jobRequirements: this.ruleForm.jobRequirements,
              education: this.ruleForm.education,
              workYear: this.ruleForm.workYear,
              socialBenefits: this.ruleForm.socialBenefits,
              pay: this.ruleForm.pay,
              workPlace: this.ruleForm.workPlace,
            })
            .then((res) => {
              if (res.data.success == true) {
                this.$message.success("发布成功!");
                this.resetForm();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查招聘信息是否完整！");
          return false;
        }
      });
    },

    // 清空表单
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },

  created() {
    // 取出用户信息
    this.userinfo = JSON.parse(window.localStorage.getItem("userinfo"));
    if (this.userinfo) {
      this.username = this.userinfo.name;
    }
    console.log(this.userinfo);
  },
};
</script>

<style lang="scss" scoped >
* {
  padding: 0;
  margin: 0;
}
.addJob {
  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 435px;
    margin: 0 auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 30px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 30px;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }
  ::v-deep .el-button {
    width: 100px;
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
}
</style>
